package cn.bugDance.domain.credit.service.adjust.policy.impl;

import cn.bugDance.domain.credit.model.aggregate.CreditAdjustAggregate;
import cn.bugDance.domain.credit.model.entity.TaskEntity;
import cn.bugDance.domain.credit.model.entity.TradeEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditAccountEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditOrderEntity;
import cn.bugDance.domain.credit.model.valobj.CreditPayVO;
import cn.bugDance.domain.credit.model.valobj.TradeNameVO;
import cn.bugDance.domain.credit.model.valobj.TradeTypeVO;
import cn.bugDance.domain.credit.repository.ICreditRepository;
import cn.bugDance.domain.credit.service.adjust.policy.IAdjustPolicy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("CONVERT_SKU")
@Slf4j
public class ConvertSkuAdjustPolicy implements IAdjustPolicy {

    @Resource
    private ICreditRepository creditRepository;


    @Override
    public String adjust(TradeEntity tradeEntity) {

        String userId = tradeEntity.getUserId();
        String bizId = tradeEntity.getBizId();
        Integer tradeAmount = tradeEntity.getTradeAmount();
        TradeNameVO tradeName  = tradeEntity.getTradeName();
        TradeTypeVO tradeType = tradeEntity.getTradeType();

        CreditPayVO creditPay = new CreditPayVO();
        creditPay.setUserId(userId);
        creditPay.setOutBusinessId(bizId);

        UserCreditAccountEntity userCreditAccountEntity = CreditAdjustAggregate.createUserCreditAccountEntity(userId, tradeAmount, tradeType);
        UserCreditOrderEntity userCreditOrderEntity = CreditAdjustAggregate.createUserCreditOrderEntity(userId, tradeName, tradeType, tradeAmount, bizId);
        TaskEntity taskEntity = CreditAdjustAggregate.createTaskEntity(userId, creditPay);

        CreditAdjustAggregate creditAdjustAggregate = CreditAdjustAggregate.builder()
                .userId(userId)
                .userCreditAccountEntity(userCreditAccountEntity)
                .userCreditOrderEntity(userCreditOrderEntity)
                .taskEntity(taskEntity).build();

        creditRepository.createUserCreditOrder(creditAdjustAggregate);

        return userCreditOrderEntity.getOrderId();
    }
}
